package PAT;

import java.util.*;
import java.util.function.Consumer;

class N{
    String to;
    int value;

    public N(String to, int value) {
        this.to = to;
        this.value = value;
    }
}

public class PAT1034 {
    private static List<String> ans = new ArrayList<>();
    private static List<Integer> ansValue = new ArrayList<>();
    private static int limit = 0;
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        limit = scanner.nextInt();
        Map<String, List<N>> graph = new HashMap<>();
        Map<String,Boolean> visited = new HashMap<>();
        for (int i = 0; i < n; i++) {
            String a = scanner.next();
            String b = scanner.next();
            int value = scanner.nextInt();
            N node = new N(b, value);
            List<N> ns = graph.get(a);
            if(ns==null){
                ArrayList<N> ns1 = new ArrayList<>();
                ns1.add(node);
                graph.put(a,ns1);
            }else{
                graph.get(a).add(node);
            }
            visited.put(a,false);
        }
        for (Map.Entry<String, Boolean> entry : visited.entrySet()) {
            if(!entry.getValue()){
                ArrayList<String> temp = new ArrayList<>();
                dfs(visited,graph,entry.getKey(),temp);
                temp.stream().max(new Comparator<String>() {
                    @Override
                    public int compare(String o1, String o2) {
                        final int[] l = {0,0};
                        graph.get(o1).forEach(new Consumer<N>() {
                            @Override
                            public void accept(N n) {
                                l[0] += n.value;
                            }
                        });
                        graph.get(o2).forEach(new Consumer<N>() {
                            @Override
                            public void accept(N n) {
                                l[1] += n.value;
                            }
                        });
                        return l[0]-l[1];
                    }
                }).get();
            }
        }
    }

    private static void dfs(Map<String, Boolean> visited, Map<String, List<N>> graph, String key, List<String> list) {
        if(visited.get(key)) return;
        visited.put(key,true);
        List<N> ns = graph.get(key);
        for (N n : ns) {
            list.add(n.to);
            dfs(visited,graph,n.to,list);
            list.remove(list.size()-1);
        }
        visited.put(key ,false);
    }
}
